//
//  LeftViewController.m
//  XLZChat
//
//  Created by dllo on 16/10/20.
//  Copyright © 2016年 徐昊. All rights reserved.
//  抽屉界面

#import "LeftViewController.h"
#import "LeftTableViewCell.h"
#import "LeftSonViewController.h"
#import "SettingsViewController.h"
#import "AppDelegate.h"
#import "UIViewController+MMDrawerController.h"
#import "Masonry.h"
#import "TwobarcodeViewController.h"
#import <DKNightVersion/DKNightVersion.h>

@interface LeftViewController ()<UITableViewDelegate, UITableViewDataSource>
/** tableView */
@property (nonatomic, strong)UITableView *tableView;
/** tableView上面的View */
@property (nonatomic, strong)UIView *headerView;
/** tableView的数组 */
@property (nonatomic, strong)NSArray *arr;
/** 判断 */
@property (nonatomic, assign) BOOL isout;

@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.arr = @[@"我的收藏", @"我的相册"];
    _isout = NO;
    [self createHeaderView];
    [self createTableView];
    [self creatHeaderSubView];
    [self createFootView];
}

// push方法
- (void)pushViewController:(UIViewController *) viewController {
    [self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
        AppDelegate *appDlegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        // 通过mainVC中的子navVC进行跳转
        UINavigationController *nav = appDlegate.mainVC.viewControllers[appDlegate.mainVC.selectedIndex];
        [nav pushViewController:viewController animated:YES];
    }];
}

// 创建tableView上面的View
- (void)createHeaderView {
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.bounds.size.height / 3)];
   
    [self.view addSubview:_headerView];
}
// 创建headerView上面的子控件
- (void)creatHeaderSubView {
    // 头像
    UIButton *btnAvatar = [UIButton buttonWithType:UIButtonTypeCustom];
    btnAvatar.backgroundColor = [UIColor greenColor];
    btnAvatar.imageView.layer.cornerRadius = 30;
    
    [self.view addSubview:btnAvatar];
    [btnAvatar addTarget:self action:@selector(clickBtnAvatar:) forControlEvents:UIControlEventTouchUpInside];
    [btnAvatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(60);
        make.top.mas_equalTo(self.view.mas_top).offset(_headerView.frame.size.height / 3);
        make.left.mas_equalTo(self.view.frame.size.width / 15);
    }];
    
    // 网名
    UIButton *btnName = [UIButton buttonWithType:UIButtonTypeCustom];
    btnName.backgroundColor = [UIColor redColor];
    [self.view addSubview:btnName];
    [btnName addTarget:self action:@selector(clickBtnAvatar:) forControlEvents:UIControlEventTouchUpInside];
    [btnName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(btnAvatar.mas_right).offset(10);
        make.width.mas_equalTo(self.view.bounds.size.width / 3);
        make.top.mas_equalTo(btnAvatar.mas_top);
        make.height.mas_equalTo(btnAvatar.mas_height);
    }];
    // 二维码
     UIButton *btn2D = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn2D setImage:[UIImage imageNamed:@"share_qrcode_icon"] forState:UIControlStateNormal];
    [self.view addSubview:btn2D];
    [btn2D addTarget:self action:@selector(clickBtn2D:) forControlEvents:UIControlEventTouchUpInside];
    [btn2D mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(btnName.mas_right).offset(10);
        make.width.mas_equalTo(self.view.bounds.size.width / 6);
        make.height.mas_equalTo(btnAvatar.mas_height);
        make.centerY.mas_equalTo(btnAvatar.mas_centerY);
    }];
    // 个性签名
    UILabel *labelSignature = [[UILabel alloc] init];
    labelSignature.backgroundColor = [UIColor cyanColor];
    labelSignature.numberOfLines = 0;
    labelSignature.textAlignment = 1;
    labelSignature.textColor = [UIColor blackColor];
    labelSignature.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:labelSignature];
    [labelSignature mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.top.mas_equalTo(btnAvatar.mas_bottom).offset(10);
        make.height.mas_greaterThanOrEqualTo(50);
}];
}
// 创建tableView
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height / 3, self.view.bounds.size.width, self.view.bounds.size.height / 3 * 2 - 50)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[LeftTableViewCell class] forCellReuseIdentifier:@"cell"];
}
// tableView下面的View
- (void)createFootView {
    UIView *footView = [[UIView alloc] init];
    footView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:footView];
    [footView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_tableView.mas_bottom);
        make.width.mas_equalTo(self.view.mas_width);
        make.height.mas_equalTo(50);
        make.bottom.mas_equalTo(self.view.mas_bottom);
    }];
    // 设置按钮
    UIButton *btnSet = [UIButton buttonWithType:UIButtonTypeCustom];
    btnSet.backgroundColor = [UIColor redColor];
    [btnSet setTitle:@"设置" forState:UIControlStateNormal];
    [btnSet setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:btnSet];
    [btnSet addTarget:self action:@selector(clickBtnSet:) forControlEvents:UIControlEventTouchUpInside];
    [btnSet mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.frame.size.width / 15);
        make.width.mas_equalTo(self.view.frame.size.width / 4.5);
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(footView.mas_top).offset(5);
    }];
    // 夜间模式按钮
    UIButton *btnNight = [UIButton buttonWithType:UIButtonTypeCustom];
    btnNight.backgroundColor = [UIColor redColor];
    [btnNight setTitle:@"夜间模式" forState:UIControlStateNormal];
    [btnNight setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:btnNight];
    [btnNight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(footView.mas_right).offset(-self.view.frame.size.width / 15);
        make.width.height.mas_equalTo(btnSet);
        make.centerY.mas_equalTo(btnSet.mas_centerY);
    }];
    [btnNight addTarget:self action:@selector(clickBtnNight:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.bounds.size.height / 15;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.nameLabel.text = _arr[indexPath.row];
    return cell;
}
#pragma mark - 按钮点击方法
// 头像网名
- (void)clickBtnAvatar:(UIButton *)btn {
    LeftSonViewController *sonVC = [[LeftSonViewController alloc] init];
    sonVC.hidesBottomBarWhenPushed = YES;
    [self pushViewController:sonVC];
}
// 二维码
- (void)clickBtn2D:(UIButton *)btn2D {
    TwobarcodeViewController *twoVC = [[TwobarcodeViewController alloc] init];
    twoVC.hidesBottomBarWhenPushed = YES;
    [self pushViewController:twoVC];
}
// 设置按钮
- (void)clickBtnSet:(UIButton *)btnSet {
    SettingsViewController *settingVC = [[SettingsViewController alloc] init];
    settingVC.hidesBottomBarWhenPushed = YES;
    [self pushViewController:settingVC];
}
// 夜间模式按钮
- (void)clickBtnNight:(UIButton *)btnNight {
    [btnNight removeTarget:self action:@selector(clickBtnNight:) forControlEvents:UIControlEventTouchUpInside];
    [self.dk_manager nightFalling];
    [btnNight addTarget:self action:@selector(clickBtnRemove:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)clickBtnRemove:(UIButton *)removeBtn {
    [removeBtn removeTarget:self action:@selector(clickBtnRemove:) forControlEvents:UIControlEventTouchUpInside];
    [removeBtn addTarget:self action:@selector(clickBtnNight:) forControlEvents:UIControlEventTouchUpInside];
    [self.dk_manager dawnComing];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
